<template>
  <scroll class="singer_main" ref="listview" :data="data" :listenScroll="listenScroll" :probeType="probeType" @scroll="scroll"
    >
    <div class="demo-infinite-container">
      <ul v-for="(item,$index) in data" ref="ListGrup">
        <li class="list-group">
          <h2 class="list-group-title">{{item.title}}</h2>
          <ul>
            <li v-for="(singer,$index) in item.items" @click="selectItem(singer,$event)" class="list-group-item ripplediv">
              <img class="avatar" v-lazy="singer.avatar">
              <span class="singername">{{singer.name}}</span>
              <div class='ripple' @click.stop></div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <div class="singer_group" v-if="shortcutList" @touchstart.stop.prevent="onShortcutTouchStart" @touchmove.stop.prevent="onShortcutTouchMove"
      @touchend.stop>
      <ul ref="groupul">
        <li v-for="(item,$index) in shortcutList" :data-index="$index" v-bind:class="{current:$index===currentIndex}">
          {{item}}
        </li>
      </ul>
    </div>
    <div class="List-fixed" v-show="fixedTitle" ref="fixed">
      <h1 class="fixed-title">{{fixedTitle}}</h1>
    </div>
    <div class="loading-container" v-show="!data.length>0">
      <loading></loading>
    </div>
  </scroll>
</template>
<script>
  import loading from "@/base/loading"
  import scroll from "@/base/scroll"
  import {
    getData,
    addClass,
    removeClass
  } from "@/assets/js/dom"
  const ANCHOR_HEIGHT = 18
  const TITLE_HEIGHT = 28
  export default {
    name: "component_name",
    props: {
      data: {
        type: Array,
        default: []
      }
    },
    data() {
      return {
        scrollY: -1,
        currentIndex: 0,
        diff: -1
      };
    },
    methods: {
      onShortcutTouchStart(e) {
        let anchorIndex = getData(e.target, 'index')
        let firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY
        this.touch.anchorIndex = anchorIndex
        this._scrollTo(anchorIndex)
      },
      onShortcutTouchMove(e) {
        let firstTouch = e.touches[0]
        this.touch.y2 = firstTouch.pageY
        let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
        let anchorIndex = parseInt(this.touch.anchorIndex) + delta
        this._scrollTo(anchorIndex)
      },
      scroll(pos) {
        this.scrollY = pos.y
      },
      _scrollTo(index) {
        if (!index && index !== 0) {
          return
        }
        if (index < 0) {
          index = 0
        } else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length - 2
        }
        this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollToElement(this.$refs.ListGrup[index], 0)
      },
      _calculateHeight() {
        this.listHeight = []
        const list = this.$refs.ListGrup
        let height = 0;
        this.listHeight.push(height)
        for (let i = 0; i < list.length; i++) {
          let item = list[i]
          height += item.clientHeight
          this.listHeight.push(height)
        }
      },
      selectItem(singer,e) {
        var clicked = e.target;
        if (e.target.className.indexOf("ripplediv") < 0) {
          clicked = e.target.parentNode
        }
        var relativeX = (e.pageX - clicked.offsetLeft);
        var relativeY = (e.pageY - clicked.offsetTop);
        var width = clicked.offsetWidth;
        var ripple = clicked.querySelector('.ripple');
        addClass(ripple, "notransition");
        ripple.style.left = relativeX + "px";
        ripple.offsetHeight;
        removeClass(ripple, "notransition");
        addClass(clicked, "hovered");
        ripple.style.width = width * 2 + "px";
        ripple.style.height = width * 2 + "px";
        ripple.style.marginLeft = -width + "px";
        ripple.style.marginTop = -width + "px";
        setTimeout(function () {
          addClass(ripple, "notransition");
          ripple.setAttribute("style", "");
          ripple.offsetHeight;
          removeClass(clicked, "hovered");
          removeClass(ripple, "notransition");
        }, 600);
        this.$emit('select',singer)
      },
    },
    computed: {
      shortcutList() {
        if (this.data.length == 0) {
          return false
        }
        return this.data.map((group) => {
          return group.title.substr(0, 1)
        })
      },
      fixedTitle() {
        if (this.scrollY > 0) {
          return ''
        }
        return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
      }
    },
    created() {
      this.touch = {}
      this.listenScroll = true
      this.listHeight = []
      this.probeType = 3
    },
    watch: {
      data() {
        setTimeout(() => {
          this._calculateHeight()
        }, 20)
      },
      scrollY(newY) {
        const listHeight = this.listHeight
        //滚动到顶部，newY>0
        if (newY > 0) {
          this.currentIndex = 0
          return
        }
        //中间部分滚动
        for (let i = 0; i < listHeight.length - 1; i++) {
          let height1 = listHeight[i]
          let height2 = listHeight[i + 1]
          if (-newY >= height1 && -newY < height2) {
            this.currentIndex = i
            this.diff = height2 + newY
            return
          }
        }
        //底部滚动
        this.currentIndex = listHeight.length - 2
      },
      diff(newVal) {
        let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
        if (this.fixedTop === fixedTop) {
          return
        }
        this.fixedTop = fixedTop
        console.log(this.$refs.fixed)
        this.$refs.fixed.style.transform = 'translate3d(0,' + fixedTop + 'px,0)'
      }
    },
    components: {
      scroll,
      loading
    }
  }

</script>
<style lang="scss">
  .singer_main {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    .singer_group {
      position: absolute;
      width: 26px;
      right: 6px;
      top: 50%;
      transform: translateY(-50%);
      z-index: 9999;
      border-radius: 6px;
      background: rgb(237, 237, 237);
      text-align: center;
    }
    ul {
      margin: 0px;
      padding: 0px;
      .current {
        color: red;
      }
      li {
        list-style-type: none;
        font-size: 12px;
      }
    }
    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%)
    }
  }

  .List-fixed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    .fixed-title {
      margin: 0;
      padding: 0;
      height: 30px;
      line-height: 30px;
      padding-left: 20px;
      font-size: 14px;
      color: #fff;
      background: #474a4f;
    }
  }

  .demo-infinite-container {
    -webkit-overflow-scrolling: touch;
    .list-group-title {
      margin: 0;
      padding: 0;
      height: 30px;
      line-height: 30px;
      padding-left: 20px;
      font-size: 14px;
      font-weight: 400;
      background: #474a4f;
      color: #fff;
    }
    .list-group-item {
      position: relative;
      overflow: hidden;
      height: 70px;
      display: flex;
      align-items: center;
      padding: 20px 0 15px 15px;
      .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      .singername {
        padding-left: 12px;
      }
    }
  }



  .ripple {
    /*stylings for the circular overlay*/
    position: absolute;
    border-radius: 100%;
    width: 0;
    height: 0;
    background: rgba(0, 0, 0, .05);
    transition: all .9s ease-out;
    top: 35px;
  }

  .notransition {
    /*used to reset the ripple without an animatiion*/
    -webkit-transition: none !important;
    transition: none !important;
  }

</style>
